package cn.com.mall.ware.service.impl;

import cn.com.mall.common.dto.SkuHasStockVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.Query;

import cn.com.mall.ware.dao.WmsWareSkuDao;
import cn.com.mall.ware.entity.WmsWareSkuEntity;
import cn.com.mall.ware.service.WmsWareSkuService;
import org.springframework.util.StringUtils;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String skuId=(String)params.get("skuId");
        String wareId=(String)params.get("wareId");
        QueryWrapper<WmsWareSkuEntity> wmsWareSkuEntityQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(skuId) && !"0".equals(wareId)){
            wmsWareSkuEntityQueryWrapper.eq("sku_id",skuId);
        }
        if(!StringUtils.isEmpty(wareId) && !"0".equals(wareId)){
            wmsWareSkuEntityQueryWrapper.eq("ware_id",skuId);
        }

        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),wmsWareSkuEntityQueryWrapper

        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(sku -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();

            long count = baseMapper.getSkuStock(sku);
            skuHasStockVo.setSkuId(sku);
            skuHasStockVo.setHasStock(count > 0);

            return skuHasStockVo;

        }).collect(Collectors.toList());

        return collect;
    }

}